﻿using UnityEditor;
using System;
using System.Diagnostics;
using System.IO;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
#if UNITY_EDITOR
using UnityEditor.iOS.Xcode;
#endif
using UnityEditor.Build.Reporting;
using UnityEditor.Build;
using System.Linq;


public class MBuildEvent : Editor, IPreprocessBuildWithReport, IPostprocessBuildWithReport
{
    public int callbackOrder => 0;
    static Dictionary<string, string> dicDefaulttContent = new Dictionary<string, string>();
    static bool bResume = false;
    static BuildReport preProgress;
    //打包前处理
    public void OnPreprocessBuild(BuildReport report)
    {
        UnityEngine.Debug.Log("打包开始, 当前文件夹:" + Directory.GetCurrentDirectory());
        Dictionary<string, string> dicEncrypt = new Dictionary<string, string>();
        if (Directory.Exists(report.summary.outputPath))
        {// 打包前清空目标文件夹
            foreach (var it in Directory.EnumerateFiles(report.summary.outputPath, "*", SearchOption.AllDirectories))
            {
                try
                {
                    File.Delete(it);
                }
                catch (System.Exception e)
                {
                    UnityEngine.Debug.LogError("清空打包文件夹失败, 打包失败: " + e.Message);
                    return;
                }
            }
        }
    }


    //打包后处理
    public void OnPostprocessBuild(BuildReport report)
    {
        UnityEngine.Debug.Log("打包完成");
    }
}

public static class BuildLater
{

    [UnityEditor.Callbacks.PostProcessBuild]
    public static void OnPostProcessBuild(BuildTarget buildTarget, string path)
    {

        switch (buildTarget)
        {
            case BuildTarget.Android:
                CopyToAndroid(path);
                break;
            case BuildTarget.iOS:
                CopyToMac(path);
                break;
            default:
                UnityEngine.Debug.LogError("未知编译平台: " + buildTarget.ToString());
                break;
        }

    }

    static void CopyToAndroid(string projectPath)
    {
        var projectFolder = projectPath.Split("/"[0]);
        string strProjectPath = string.Empty;
        for (int i = 0; i < projectFolder.Length - 2; i++)
        {
            strProjectPath += projectFolder[i] + "\\";
        }
        Process proc = null;
        try
        {
            string targetDir = string.Format(strProjectPath);//this is where testChange.bat lies
            proc = new Process();
            proc.StartInfo.WorkingDirectory = targetDir;
            proc.StartInfo.FileName = "androidBuild.bat";
            proc.StartInfo.Arguments = projectPath;
            proc.Start();
            proc.WaitForExit();
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString());
        }
    }

    static void CopyToMac(string projectPath)
    {
        var projectFolder = projectPath.Split("/"[0]);
        string strProjectPath = string.Empty;
        for (int i = 0; i < projectFolder.Length - 2; i++)
        {
            strProjectPath += projectFolder[i] + "/";
        }
        Process proc = null;
        try
        {
            string targetDir = string.Format(strProjectPath);//this is where testChange.bat lies
            proc = new Process();
            proc.StartInfo.WorkingDirectory = targetDir;
            proc.StartInfo.FileName = "iosBuild.bat";
            proc.StartInfo.Arguments = projectFolder[projectFolder.Length - 1] + " " + projectFolder[projectFolder.Length - 1];
            proc.Start();
            proc.WaitForExit();
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString());
        }
    }
}
